import { _decorator, Color, Component, Label, Node, Sprite } from 'cc';
import { Report, ReportReadState } from '../entity/Report';
import { Utils } from '../common/Utils';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { ReportDetail } from './ReportDetail';
import ReportApi from '../api/ReportApi';
const { ccclass, property } = _decorator;

@ccclass('ReportItem')
export class ReportItem extends Component {
    @property(Sprite)
    bg:Sprite
    @property(Label)
    tittle:Label
    @property(Label)
    time:Label
    @property(Node)
    redDot:Node

    private reportData:Report
    start() {
        this.node.on(Node.EventType.TOUCH_END, this.onDetail, this)
    }

    setData(report:Report) {
        this.reportData = report

        this.tittle.string = report.title
        this.time.string = Utils.formatDate(new Date(report.startTime * 1000) )
        
        if(report.isUnread == ReportReadState.Read){
            this.redDot.active = false
        } else {
            this.redDot.active = true
        }
    }

    async onDetail(){
        let ui = await UIManager.instance.openUI(PrefabEnum.ReportDetail)
        ui.getComponent(ReportDetail).setData(this.reportData)
        // 告诉服务端，这条数据要设置成已读
        ReportApi.execute({id: this.reportData.sn})
        this.redDot.active = false
    }
}


